/*
 * ADConverter.cpp
 *
 * Created: 2012-05-16 09:13:35
 *  Author: Johan
 */
#include "ADConverter.h"

ADConverter::ADConverter(){
	PORTF = 0;
	//???
	DDRF &= ~(1<<PF0);
	//AVCC with external capacitor on AREF pin
	ADMUX|=(0<<REFS1)|(1<<REFS0);
	//ADC Enable, turns on the ADC
	ADCSRA |= (1<<ADEN);
	//Set Prescaler to 128
	ADCSRA |= (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0)|(1<<ADIE);
	ADCSRB |= (1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0);
			
	TCCR1A = (0<<WGM31)|(0<<WGM30);
	TCCR1B = (1<<WGM33)|(1<<WGM32)|(0<<CS32)|(1<<CS31)|(0<<CS30); //Prescaler
	
	ICR1 = 100;
	
	TIMSK1 |= (1<<ICIE1);
	
	
}

void ADConverter::startConversion(){
	ADCSRA |= (1<<ADSC);
}

void ADConverter::setAutoTrigger(bool b){
	if(b){
		ADCSRA |= (1<<ADATE);
	} else {
		ADCSRA &= ~(0<<ADATE);
	}
}
